package com.suitsoft.ut.web.system;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.account.Authority;
import com.suitsoft.ut.entity.system.TimeAuthority;
import com.suitsoft.ut.service.account.AccountManager;
import com.suitsoft.ut.service.commons.TimeControlManager;

/**
 * 时间控制 设置
 *@author Suineg
 */
public class TimeControlAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private AccountManager accountManager;
	private TimeControlManager controlManager;
	
	private List<Authority> authorities;
	private Long timeId;
	private Long authId;
	private Authority authority;
	private TimeAuthority timeAuthority;
	private Date startDate;
	private Date endDate;
	private Integer used;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute()  {
		authorities = accountManager.getAllAuthForMap().get(Constants.CN_TIME_CONTROL);
		//排序一下
		Set<TimeAuthority> set = null;
		for(Authority a : authorities){
			set = new TreeSet<TimeAuthority>(new Comparator(){
				@Override
				public int compare(Object o1, Object o2) {
					TimeAuthority t1 = (TimeAuthority)o1;
					TimeAuthority t2 = (TimeAuthority)o2;
					return t1.getStartDate().compareTo(t2.getStartDate());
				}
			});
			for(TimeAuthority ta : a.getTimeAuthority()){
				set.add(ta);
			}
			a.setTimeAuthority(set);
		}
		return SUCCESS;
	}
	
	@LogAnnotation(log = "时间控制 - 保存")
	public void save(){
		try {
			String msg = "";
			PrintWriter out = Struts2Utils.getResponse().getWriter();
			
			if(authId == null || authId == 0l){
				msg = "非法操作！";
			}else{
				try {
					authority = accountManager.getAuthority(authId);
					if(timeId == null || timeId == 0l){
						timeAuthority = new TimeAuthority();
					}else{
						timeAuthority = controlManager.get(timeId);
					}
					timeAuthority.setAuthority(authority);
					timeAuthority.setEndDate(endDate);
					timeAuthority.setStartDate(startDate);
					authority.getTimeAuthority().add(timeAuthority);
					
					accountManager.saveAuthority(authority);
					controlManager.save(timeAuthority);
				} catch (Exception e) {
					e.printStackTrace();
					msg = "保存失败!";
				}
			}
			out.print(msg);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@LogAnnotation(log = "时间控制 - 删除")
	public String del(){
		if(timeId == null || timeId == 0l){
			addActionMessage("非法操作！");
			return execute();
		}
		timeAuthority = controlManager.get(timeId);
		if(timeAuthority == null){
			addActionMessage("非法操作！");
			return execute();
		}
		authority = timeAuthority.getAuthority();
		authority.getTimeAuthority().remove(timeAuthority);
		
		accountManager.saveAuthority(authority);
		controlManager.delete(timeId);
		addActionMessage("删除成功！");
		return execute();
	}
	
	/**
	 * 设置生效状态
	 */
	@LogAnnotation(log = "时间控制 - 更改生效状态")
	public void setEffective(){
		try {
			PrintWriter out = Struts2Utils.getResponse().getWriter();
			String msg = "";
			try {
				if(authId == null || authId == 0l){
					msg = "非法操作！";
					throw new Exception();
				}
				
				authority = accountManager.getAuthority(authId);
				authority.setEffective(used);
				accountManager.saveAuthority(authority);
				
			} catch (Exception e) {
				out.print(msg);
				e.printStackTrace();
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	public List<Authority> getAuthorities() {
		return authorities;
	}
	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}
	public Long getTimeId() {
		return timeId;
	}
	public void setTimeId(Long timeId) {
		this.timeId = timeId;
	}
	public Long getAuthId() {
		return authId;
	}
	public void setAuthId(Long authId) {
		this.authId = authId;
	}
	public TimeAuthority getTimeAuthority() {
		return timeAuthority;
	}
	public void setTimeAuthority(TimeAuthority timeAuthority) {
		this.timeAuthority = timeAuthority;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	@Autowired
	public void setControlManager(TimeControlManager controlManager) {
		this.controlManager = controlManager;
	}
	public Integer getUsed() {
		return used;
	}
	public void setUsed(Integer used) {
		this.used = used;
	}
}

